﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <list>
#include <windows.h>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Error.h"

#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/NetEngine_Core.lib")

int main()
{
	XNETHANDLE xhToken;
	LPCTSTR lpszClientID = _T("qyt");

	if (!NetCore_DataCacheEx_Init(&xhToken, 3, 0))
	{
		printf("NetCore_DataCacheEx_Init:error %X\n",NetCore_GetLastError());
		return -1;
	}
	if (!NetCore_DataCacheEx_Create(xhToken,lpszClientID))
	{
		printf("NetCore_DataCacheEx_Create:error %X\n", NetCore_GetLastError());
		return -1;
	}

	for (int i = 0; i < 5; i++)
	{
		LPCTSTR lpszMsgBuffer = _T("hello world\n");
		int nLen = _tcslen(lpszMsgBuffer);
		
		if (!NetCore_DataCacheEx_Send(xhToken,lpszClientID, lpszMsgBuffer, nLen))
		{
			printf("NetCore_DataCacheEx_Send:error %X\n", NetCore_GetLastError());
			return -1;
		}
	}

	int nIndex = 1;
	for (int i = 0;i < 5;i++)
	{
		int nMsgLen = 2048;
		TCHAR tszMsgBuffer[2048];
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

		if (NetCore_DataCacheEx_Recv(xhToken, lpszClientID, tszMsgBuffer, &nMsgLen, &nIndex))
		{
			printf("NetCore_DataCacheEx_Recv: %s\n", tszMsgBuffer);
		}
		else
		{
			printf("NetCore_DataCacheEx_Recv:error %X\n", NetCore_GetLastError());
		}
		nIndex++;
	}
	NetCore_DataCacheEx_Close(xhToken, lpszClientID);
	NetCore_DataCacheEx_Destory(xhToken);

	return 0;
}